import { Component, OnInit, Input, Output, EventEmitter, OnChanges, SimpleChanges, ChangeDetectionStrategy } from '@angular/core';
import { ShareInfo } from '@store/reducers/member.reducer';
import { FormGroup, FormControl, Validators } from '@angular/forms';
import { NzMessageService } from 'ng-zorro-antd';
import { ShareParams } from '@services/member.service';

@Component({
  selector: 'app-wyy-layer-share',
  templateUrl: './wyy-layer-share.component.html',
  styleUrls: ['./wyy-layer-share.component.less'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class WyyLayerShareComponent implements OnInit, OnChanges {
  @Input() visible: boolean;
  @Input() shareInfo: ShareInfo;
  @Output() onShare = new EventEmitter<ShareParams>();
  @Output() onCancelShare = new EventEmitter<void>();
  formModel: FormGroup;
  constructor(private messageService: NzMessageService) {
    this.formModel = new FormGroup({
      msg: new FormControl('', Validators.maxLength(140))
    });
  }

  ngOnInit() {
  }
  ngOnChanges(changes: SimpleChanges) {
    const visible = changes.visible;
    // 弹窗关闭后，表单重置
    if (visible && !visible.previousValue) {
      this.formModel.reset();
    }
  }
  cancelShare() {
    this.onCancelShare.emit();
  }
  onSubmit() {
    if (!this.formModel.value.msg || !this.formModel.value.msg.trim().length) {
      this.messageService.create('warning', '分享内容不能为空');
      return;
    }
    if (this.formModel.valid) {
      if (this.shareInfo) {
        this.onShare.emit({
          id: this.shareInfo.id,
          msg: this.shareInfo.txt,
          type: this.shareInfo.type
        });
      }
    }
  }
}
